﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace laohan.Common.USB.DeviceNotify
{
	internal sealed class DevNotifyNativeWindow : NativeWindow
	{
		internal delegate void OnDeviceChangeDelegate(ref Message m);

		internal delegate void OnHandleChangeDelegate(IntPtr windowHandle);

		private const string WINDOW_CAPTION = "{18662f14-0871-455c-bf99-eff135425e3a}";

		private const int WM_DEVICECHANGE = 537;

		private readonly OnDeviceChangeDelegate mDelDeviceChange;

		private readonly OnHandleChangeDelegate mDelHandleChanged;

		internal DevNotifyNativeWindow(OnHandleChangeDelegate delHandleChanged, OnDeviceChangeDelegate delDeviceChange)
		{
			mDelHandleChanged = delHandleChanged;
			mDelDeviceChange = delDeviceChange;
			CreateHandle(new CreateParams
			{
				Caption = "{18662f14-0871-455c-bf99-eff135425e3a}",
				X = -100,
				Y = -100,
				Width = 50,
				Height = 50
			});
		}

		protected override void OnHandleChange()
		{
			mDelHandleChanged(base.Handle);
			base.OnHandleChange();
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == 537)
			{
				mDelDeviceChange(ref m);
			}
			base.WndProc(ref m);
		}
	}
}
